<?php
// Returns an XML list of all inquiries with their name, email, whether they
// have applied or not, and the date of last contact.

session_start();
if ( ! isset( $_SESSION['adminLoggedIn'] ) ) {
   exit;
}

require_once("db.php");

$sql = '
SELECT gs_inquiries.id as primaryKey, 
   gs_inquiries.name,
   ndgs,
   gs_inquiries.email, 
   max(gs_followups.date) as lastContact,
   count(gs_followups.date) as numContacts
FROM gs_inquiries inner join gs_followups 
   on gs_inquiries.id=gs_followups.inquiry_id 
WHERE applied=0 AND given_up=0
GROUP BY name
ORDER BY lastContact, numContacts ASC
';

$result = my_mysql_query($sql);

// Output the result as XML.
echo "<inquiries>\n";
while ($row = mysql_fetch_assoc($result)) {
   echo "<inquiry>\n";
   foreach (array_keys($row) as $key) {
      echo "<$key>"; echo $row[$key]; echo "</$key>\n";
   }
   echo "</inquiry>\n";
}
echo "</inquiries>\n";

?>